<template>
  <div class="template-form-Lu">
    <a-modal
      :visible="applicationFormVisible"
      title=""
      :maskClosable="false"
      :closable="true"
      :destroyOnClose="true"
      width="100%"
      :dialog-style="{ margin: '0 auto'}"
      wrapClassName="form-modal-Lu"
      @cancel="closeVisible"
      >
      <template slot="footer">
        <div v-if="linkName == '申请'&&this.form.fblzt!=='退回'">
          <a-button style="margin-right: 15px;" @click="saveTable('保存')">保存</a-button>
          <a-button :loading="loading" class="blue-btn" @click="saveTable('提交')">提交</a-button>
        </div>
        <div v-else-if="linkName == '我的已办'">
            <a-button class="blue-btn" @click="closeVisible">关闭</a-button>
        </div>
        <div v-else>
          <a-button @click="saveAppTable('保存')">保存</a-button>
          <a-button class="blue-btn" @click="subAppTable('提交')">提交</a-button>
          <a-button class="red-btn" @click="returnClick">退回</a-button>
        </div>
      </template>
      <a-tabs type="card" @change="callback">
        <a-tab-pane v-if="linkName=='申请'" key="0" tab="申请表">
          <div style="height: 90%; width: 100%;">
            <div style="border: 1px solid #d9d9d9;height: 100%;width: 100%;display: flex;">
                <div style="width: 45%">
                  <div style="padding: 5px;">
                    <a-form layout="inline" style="margin-left: 10px;">
                        <a-form-item label="物品名称">
                            <a-input v-model="kcName"/>
                        </a-form-item>
                        <a-form-item>
                            <a-button class="blue-btn" style="margin-right: 5px" @click="searchKclist">
                                查询
                            </a-button>
                            <a-button @click="getList">
                                重置
                            </a-button>
                        </a-form-item>
                    </a-form>
                  </div>
                    <a-table
                    :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
                    :columns="columnsB"
                    :data-source="dataB"
                    />
                </div>
                <div class="wageLogsDiv-center">
                    <a-button class="blue-btn" @click="resetting" style="margin-bottom: 10px;">重置</a-button>
                    <br/>
                    <a-button class="blue-btn" @click="deletes">批量删除</a-button>
                </div>
                <div style="width: 45%;">
                    <div style="padding: 10px;">
                        <p>已选物品： {{ checkListName }}</p>
                    </div>
                    <a-table bordered :data-source="dataSource" :columns="columns" :row-selection="{ selectedRowKeys: listSelectedRowKeys, onChange: listOnSelectChange }">
                    <template slot="name" slot-scope="text, record">
                        <a-input v-if="record.isEditFsl" v-model="record.fsl" :key="0" type="number" style="width: 100px;"></a-input>
                        <span v-else :key="1">{{ record.fsl }}</span>
                        <a
                        href="javascript:;"
                        @click="editFslFuntion(record, 'fsl', 'isEditFsl','isEdit')"
                        style="margin-left: 20px;">
                        {{ record.isEdit }}
                        </a>
                    </template>
                    <template slot="operation" slot-scope="text, record">
                        <a href="javascript:;" @click="onDelete(record)">删除</a>
                    </template>
                    </a-table>
                </div>
            </div>
            <div style="margin-top: 1%">
                <a-textarea v-model="form.fbz" placeholder="请输入备注：" :rows="2" />
            </div>
          </div>
        </a-tab-pane>
        <a-tab-pane v-else key="1" tab="审批表">
          <div class="print-div-shu">
            <a-form-model
                ref="ruleForm"
                :model="form"
                :rules="rules"
                >
                <h1 class="h1-page">xx壮族自治区食品药品审评查验中心<br/>办公用品申请审批表</h1>
                <table border="1" cellspacing="0" class="table-Lu bgyp-table">
                    <tr>
                        <th>申请部门</th>
                        <td>{{ form.flyks }}</td>
                        <th>申请人</th>
                        <td>{{ form.flyr }}</td>
                        <th>申请时间</th>
                        <td>{{ form.flyrq }}</td>
                    </tr>
                    <tr>
                        <th>备注</th>
                        <td colspan="5" class="text-align-left">
                            <!-- <p class="text-align-left">{{ form.fbz }}</p> -->
                            {{ form.fbz }}
                        </td>
                    </tr>
                    <tr>
                        <td colspan="6">
                            <a-table :columns="columnsMx" :data-source="Mxlist" :pagination="false" class="bgyp-appform-table">
                            </a-table>
                        </td>
                    </tr>
                    <tr>
                        <th>科室意见</th>
                        <td v-if="linkName == '科室审批' || linkName == '数据修订'" colspan="5">
                            <a-form-model-item ref="fksyj" prop="fksyj">
                                <reviewOpinions
                                  ref="reviewOpinionsRefKs"
                                  @getInputOpinion="getInputOpinion"
                                  @openOpinions="ksOpinions"
                                  v-decorator="[
                                    'fksyj', {initialValue:form.fksyj,rules: rules.fksyj}]"/>
                            </a-form-model-item>
                            <div v-if="linkName == '数据修订'" class="yjqm-right-bottom">
                                <a-input
                                    v-model="form.fksyjr"
                                    @click="checkMan('fksyjr',form.fksyjr)"
                                    class="checkMan-Xiu"
                                    >
                                </a-input>
                                <a-date-picker
                                    show-time
                                    placeholder=""
                                    v-model="form.fksyjsj"
                                    @change="timeChang($event,'fksyjsj')"
                                    class="checkTime-Xiu"
                                    />
                            </div>
                        </td>
                        <td v-else colspan="5">
                            <p class="text-align-left">{{ form.fksyj }}</p>
                            <div class="yjqm-right-bottom">
                                <img v-if="getUrl('科室审批') && form.fksyjsj" :src="getUrl('科室审批')" alt="" class="yjqmImg"/>
                                <span v-else class="yjqmImg">{{ form.fksyjr }}</span>
                                <span>{{ form.fksyjsj }}</span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th>办公室确认</th>
                        <td v-if="linkName == '办公室确认' || linkName == '数据修订'" colspan="5">
                            <a-form-model-item  ref="fbgsyj" prop="fbgsyj">
                                <reviewOpinions
                                  ref="reviewOpinionsRefBgs"
                                  @getInputOpinion="getInputOpinion"
                                  @openOpinions="bgsOpinions"
                                  v-decorator="[
                                    'fbgsyj', {initialValue:form.fbgsyj,rules: rules.fbgsyj}]"/>
                            </a-form-model-item>
                            <div v-if="linkName == '数据修订'" class="yjqm-right-bottom">
                                <a-input
                                    v-if="linkName == '数据修订'"
                                    v-model="form.fbgsyjr"
                                    @click="checkMan('fbgsyjr',form.fbgsyjr)"
                                    class="checkMan-Xiu"
                                ></a-input>
                                <a-date-picker
                                    show-time
                                    placeholder=""
                                    v-model="form.fbgsyjsj"
                                    @change="timeChang($event,'fbgsyjsj')"
                                    class="checkTime-Xiu"
                                    />
                            </div>
                        </td>
                        <td v-else colspan="5">
                            <p class="text-align-left">{{ form.fbgsyj }}</p>
                            <div class="yjqm-right-bottom">
                                <img v-if="getUrl('办公室确认') && form.fbgsyjsj" :src="getUrl('办公室确认')" alt="" class="yjqmImg"/>
                                <span v-else class="yjqmImg">{{ form.fbgsyjr }}</span>
                                <span>{{ form.fbgsyjsj }}</span>
                            </div>
                        </td>
                    </tr>
                </table>
            </a-form-model>
          </div>
        </a-tab-pane>
        <a-tab-pane v-if="linkName == '我的已办'" key="2" tab="流转记录">
          <a-table
            :columns="lzColumns"
            :data-source="lzList"
            bordered
            style="border-color: #d9d9d9;font-weight: 600;text-align: center;"
            :customRow="customRow">
          </a-table>
        </a-tab-pane>
      </a-tabs>
    </a-modal>
    <personnelList ref="personnelListRef" :modaObj="modaObj" @checkPersonnel="checkPersonnel"></personnelList>
  </div>
</template>
<script>
import {kcList,initBzFlow,queryById,addsqmx,delsqmx,updatesqmx,
        saveFlow,submitFlow,flowJkPage,downMyQm,queryAllYbJob,returnUpperzdyFlowJob} from './service/index.js'
import personnelList from '../../modal/personnelList.vue'
import moment from 'moment'
export default {
  props:{
    formData:{
      type:[Array,Object],
      default: () => {},
      required:false
    },
    Mxlist:{
      type:[Array,Object],
      default: () => {},
      required:false
    },
    linkName:{
      type:String,
      default:'',
      required:false,
    },
  },
  components:{
    personnelList,
  },
  data() {
    return {
      loading: false,
      applicationFormVisible: false,
      tabsKey: '1',
      moment:moment,
      dataSource: [],
      columns: [
      {
        title: '物品名称',
        dataIndex: 'fwpmc',
        key: 'fwpmc',
      },
      {
        title: '物品编号',
        dataIndex: 'fwpbh',
        key: 'fwpbh',
      },
      {
        title: '物品类别',
        dataIndex: 'fwplb',
        key: 'fwplb',
      },
      {
        title: '物品数量',
        key: 'fwpsl',
        dataIndex: 'fwpsl',
      },
      {
        title: '申请数量',
        key: 'fsl',
        scopedSlots: { customRender: 'name' },
      },
      {
        title: '操作',
        key: 'id',
        scopedSlots: { customRender: 'operation' },
      },
      ],
      text: String,
      value: this.text,
      editable: false,
      //----------
      dataB: [],
      columnsB: [
        {
          title: '物品编号',
          dataIndex: 'fwpbh',
          key: 'fwpbh',
        },
        {
          title: '物品名称',
          dataIndex: 'fwpmc',
          key: 'fwpmc',
        },
        {
          title: '物品类别',
          dataIndex: 'fwplb',
          key: 'fwplb',
        },
        {
          title: '物品数量',
          key: 'fwpsl',
          dataIndex: 'fwpsl',
        },
      ],
      selectedRowKeys: [], // Check here to configure the default column
      listSelectedRowKeys: [],
      form:{
        fbz: "",
        fdw: "",
        flczt: "",
        flyks: "",
        flyr: "",
        flyrq: "",
        fpm: "",
        fsffqlc: "",
        fsl: 0,
        fuserid: "",
        fwpbh: "",
        id: "",
        jobid: '',
        kcList: [
          {
            kcid: "",
            sqsl: "",
          }
        ],
        fksyj: "",
        fksyjr: "",
        fksyjsj: "",
        fbgsyj: "",
        fbgsyjr: "",
        fbgsyjsj: "",
      },
      kongForm: {},
      beiZhu: '',
      isBzBtn: false,
      bzVisible: false,
      columnsMx: [
        {
          title: '物品编号',
          dataIndex: 'fwpbh',
          key: 'fwpbh',
        },{
          title: '品名',
          dataIndex: 'fsqmc',
          key: 'fsqmc',
        },{
          title: '数量',
          dataIndex: 'fsqsl',
          key: 'fsqsl',
        },
        {
          title: '单位',
          dataIndex: 'fdw',
          key: 'fdw',
        },
      ],
      tableList:[],

      cform:this.$form.createForm(this, { name: 'cform' }),
      rules:{
        fksyj: [{
              required: true,
              message: '请输入科室意见',
              trigger: 'blur',
          }],
          fbgsyj: [{
            required: true,
            message: '请输入办公室意见',
            trigger: 'blur',
        }],
      },
      lzColumns: [
        {
          title: '环节名称',
          dataIndex: 'fhjmc',
          key: 'fhjmc',
          width: 150,
        },{
          title: '待办人',
          dataIndex: 'fdbr',
          key: 'fdbr',
          width: 100,
        },{
          title: '办理人',
          dataIndex: 'fblr',
          key: 'fblr',
          width: 100,
        },{
          title: '发送时间',
          dataIndex: 'ffssj',
          key: 'ffssj',
          width: 170,
        },{
          title: '办理时间',
          dataIndex: 'fblsj',
          key: 'fblsj',
          width: 170,
        },{
          title: '办理状态',
          dataIndex: 'fblzt',
          key: 'fblzt',
          width: 100,
        },{
          title: '办理类型',
          dataIndex: 'fbllx',
          key: 'fbllx',
          width: 300,
        },{
          title: '办理意见',
          dataIndex: 'fblyj',
          key: 'fblyj',
        },
      ],
      lzList: [],
      imgList: [
        {fhjmc:'科室审批',url: ''},
        {fhjmc:'办公室确认',url: ''},
      ],
      kcName: '',
      checkListName: '',
      modaObj: {},
    }
  },
  created() {},
  mounted(){
    this.getList()
    this.kongForm = JSON.parse(JSON.stringify(this.form))
    //console.log('kong?',this.kongForm)
  },
  methods: {
    // 右上角关闭后的回调
    closeVisible(e){
      this.$emit('success')
      this.applicationFormVisible = false
      this.dataSource = []
      this.selectedRowKeys = []
      this.form = {...this.kongForm}
      //console.log('kong',this.form)
    },
    timeChang(val,name){
        let data = this.form
        data[name] = moment(val).format('YYYY-MM-DD HH:mm:ss')
    },
    checkMan(place,man){
        this.$nextTick(_ => {
            this.modaObj = {place: place,manList: man}
            this.$refs.personnelListRef.personnelListVisible = true
        })
    },
    // 选择人员
    checkPersonnel(place,id,name){
        //console.log(place,id,name)
        this.$refs.personnelListRef.personnelListVisible = false
        //console.log('-------------------')
        const data = this.form
        data[place] = name.toString()
        let fdbrid = id.toString()
        let fdbr = name.toString()
        let Data = {
            jobid: this.form.jobid,
            fdbrid: id.toString(),
            fdbr: name.toString()
        }
    },
    callback(key) {
      // //console.log(key);
      this.tabsKey = key
    },
    getUrl(ids){
      let url = ''
      this.imgList.forEach(item => {
        if(item.fhjmc == ids){
          url = item.url
        }
      })
      // //console.log('长度',url.length)
      return url
    },
    customRow() {
      return {
        style: {
            'font-size' : '16px',
            'text-align' : 'center'
        },
      }
    },
    reviewRecordsFocusEvent(){},
    // 打开意见弹窗
    ksOpinions(){
        this.$refs.reviewOpinionsRefKs.getData({opinionType:'ksIdeaType',oldVal:this.form.fksyj,valueData:{hjmc:'fksyj'}})
    },
    bgsOpinions(){
        this.$refs.reviewOpinionsRefBgs.getData({opinionType:'bgsIdeaType',oldVal:this.form.fbgsyj,valueData:{hjmc:'fbgsyj'}})
    },
    // 意见弹窗
    getInputOpinion(val,valueData){
      let data = this.form
      data[valueData.hjmc] = val
    //   this.cform.setFieldsValue({
    //     fksyj:this.form.fksyj,
    //     fbgsyj: this.form.fbgsyj,
    //   });
    },
    onCellChange(key, dataIndex, value) {
      const dataSource = [...this.dataSource];
      const target = dataSource.find(item => item.key === key);
      if (target) {
        target[dataIndex] = value;
        this.dataSource = dataSource;
      }
    },
    onDelete(record) {
      //console.log(record)
      const dataSource = [...this.dataSource];
      if(this.form.id){
        //console.log('111',this.form.id)
        delsqmx({sqmxid:record.id}).then(res => {
          if(res.code == 200){
            this.$message.success(res.message)
            this.dataSource = dataSource.filter(item => item.key !== record.key);
            this.start(record.key)
          } else {
            this.$message.error(res.message)
          }
        })
      } else {
        //console.log('000')
        this.start(record.key)
      }
    },
    // ===
    handleChange(e) {
      const value = e.target.value;
      this.value = value;
    },
    check() {
      this.editable = false;
      this.$emit('change', this.value);
    },
    edit() {
      this.editable = true;
    },
    //---------------
    start(key) {
      // ajax request after empty completing
    //   setTimeout(() => {
        const selectedRowKeys = [...this.selectedRowKeys];
        this.selectedRowKeys = selectedRowKeys.filter(item => item !== key);
        // //console.log('selectedRowKeys',this.selectedRowKeys)
        const dataSource = [...this.dataSource];
        this.dataSource = dataSource.filter(item => item.key !== key);
        // //console.log(this.dataSource)
    //   }, 1000);
    },
    onSelectChange(selectedRowKeys,selectedRows) {
      //console.log(selectedRowKeys,selectedRows);
      //console.log('form',this.form)
      if(this.form.id){
        if(selectedRows.length < this.dataSource.length){
          this.$message.error('请从上方已生成记录表格删除事项！')
        } else {
          this.selectedRowKeys = []
          this.selectedRowKeys = selectedRowKeys;
          //console.log(this.selectedRowKeys,this.dataSource);

          let arr1 = this.dataSource;
          let arr2 = selectedRows;
          let diffArr = [];
          for(let i=0; i<arr1.length; i++) {
              let found = false;
              for(let j=0; j<arr2.length; j++) {
                  if(arr1[i].key === arr2[j].key && arr1[i].fwpbh === arr2[j].fwpbh) {
                      found = true;
                      break;
                  }
              }
              if(!found) {
                  diffArr.push(arr1[i]);
              }
          }
          for(let i=0; i<arr2.length; i++) {
              let found = false;
              for(let j=0; j<arr1.length; j++) {
                  if(arr2[i].key === arr1[j].key && arr2[i].fwpbh === arr1[j].fwpbh) {
                      found = true;
                      break;
                  }
              }
              if(!found) {
                  diffArr.push(arr2[i]);
              }
          }
          //console.log('diffArr',diffArr);

          let add = {
            kcid: diffArr[0].id,
            sqid: this.dataSource[0].fsqid,
            sl: 1
          }
          addsqmx(add).then(res => {
          //console.log(res.result.result)
          if(res.code == 200){
              this.$message.success(res.message)
              res.result.result.forEach(item => {
                if(item.fwpbh == diffArr[0].fwpbh){
                  //console.log('fwpbh',item.fwpbh)
                  item = {
                    ...item,
                    key: diffArr[0].key,
                    fwpsl: diffArr[0].fwpsl,
                    fwplb: diffArr[0].fwplb,
                    fwpmc: diffArr[0].fwpmc,
                    fsl: 1,
                    isEdit: '编辑',
                    isEditFsl: false
                  }
                  //console.log('item',item)
                  this.dataSource.push(item)
                  //console.log(this.dataSource)
                }
              })
          } else {
            this.$message.warning(res.message)
          }
        })
        }
      } else {
        this.selectedRowKeys = []
        this.selectedRowKeys = selectedRowKeys;
        if(selectedRows.length == 0){
          this.isBzBtn = false
        } else {
          this.isBzBtn = true // 打开备注按钮
        }
        // this.dataSource = []
        // selectedRows.forEach(item => {
        //   item = {
        //     ...item,
        //     fsl:1,
        //     isEdit: '编辑',
        //     isEditFsl: false
        //   }
        //   this.dataSource.push(item)
        // });
        if(this.dataSource.length == 0){
          this.dataSource = []
          selectedRows.forEach(item => {
            item = {
              ...item,
              fsl:1,
              isEdit: '编辑',
              isEditFsl: false
            }
            this.dataSource.push(item)
          });
        } else {
          //console.log(selectedRows.length,this.dataSource.length)
          if(selectedRows.length > this.dataSource.length){
            //console.log('大大大')
            let list = selectedRows
            this.dataSource.forEach((item,i) => {
              list.forEach(items => {
                if(items.id == item.id){
                  list = list.filter(itema => itema.id !== items.id)
                }
              })
            })
            //console.log(list)
            list.forEach(item => {
              item = {
                ...item,
                fsl:1,
                isEdit: '编辑',
                isEditFsl: false
              }
              this.dataSource.push(item)
            });
          } else {
            //console.log('小小小')
            let list = []
            selectedRows.forEach((item,i) => {
              this.dataSource.forEach(items => {
                if(items.id == item.id){
                  list.push(items)
                }
              })
            })
            this.dataSource = [...list]
          }
        }
      }
    },
    //--------------------
    //
    getList(){
        this.kcName = ''
        this.dataB = []
      kcList().then(res => {
        //console.log(res)
        res.result.records.forEach((item,i) => {
          item = {
            ...item,
            key: item.fwpbh,
          }
          this.dataB.push(item)
        });
      })
    },
    // 打开备注弹窗
    addBz(){
      this.beiZhu = ''
      this.bzVisible = true
    },
    // 备注
    bzHandleOk(){
      this.form.fbz = this.beiZhu
      this.bzVisible = false
    },
    // 保存
    saveTable(str){
        this.isBzBtn = false
        let data = {}
        let fpm = []
        let fwpbh = []
        let fdw = []
        let kcList = []
        //console.log(this.dataSource)
        this.dataSource.forEach(item => {
            fpm.push(item.fwpmc)
            fwpbh.push(item.fwpbh)
            fdw.push(item.fdw)
            kcList.push({kcid:item.id,sqsl:item.fsl})
        })
        if(this.form.id){
            let data = {}
            if(str == '保存'){
                data = {
                    id: this.form.id,
                    fbz: this.form.fbz,
                    fpm: fpm.toString(),
                    fwpbh: fwpbh.toString(),
                    fdw: fdw.toString(),
                    kcList: kcList
                }
                initBzFlow(data).then(res => {
                    if(res.code == 200){
                        this.$message.success(res.message)
                        // this.form.id = res.result.Sqb.id//赋值
                        // this.Mxlist = res.result.Mxlist
                    } else {
                        this.$message.warning(res.message)
                    }
                })
            } else {
                data={
                    jobid: this.form.jobid,
                    oaid: this.form.id,
                    map: {
                        id: this.form.id,
                        fbz: this.form.fbz,
                        fpm: fpm.toString(),
                        fwpbh: fwpbh.toString(),
                        fdw: fdw.toString(),
                        kcList: kcList
                    },
                    currentNode: "",
                    dbrid: "",
                    lcmbCode: "",
                    nextNode: "",
                    nextNodeId: "",
                    opinion: "",
                    smgflag: ""
                }
                if(this.dataSource.length == 0){
                  this.$message.warning('当前未选物品，请选择物品')
                  return
                }
                this.loading = true
                submitFlow(data).then(res => {
                    if(res.code == 200){
                        this.$message.success(res.message)
                        this.closeVisible()
                    } else {
                        this.$message.warning(res.message)
                    }
                    this.loading = false
                })
            }
        } else {
            data = {
                id: this.form.id,
                fbz: this.form.fbz,
                fpm: fpm.toString(),
                fwpbh: fwpbh.toString(),
                fdw: fdw.toString(),
                kcList: kcList
            }
            if(str == '提交'){
                data = {
                ...data,
                fsffqlc: '是',
                }
                this.loading = true
            }
            //console.log(data)
            if(str == '提交' && this.dataSource.length == 0){
              this.$message.warning('当前未选物品，请选择物品')
              return
            }
            initBzFlow(data).then(res => {
                if(res.code == 200){
                    this.$message.success(res.message)
                    if(str == '保存'){
                        this.form.id = res.result.oaMap.Sqb.id//赋值
                        this.form.jobid = res.result.jobMap.id//赋值
                        this.Mxlist = res.result.oaMap.Mxlist
                    } else {
                        // 提交后清空
                        this.closeVisible()
                        this.dataSource = []
                        this.selectedRowKeys = []
                        this.form = {...this.kongForm}
                        //console.log('form',this.form)
                    }
                } else {
                    this.$message.warning(res.message)
                }
                this.loading = false
            })
        }
    },
    saveAppTable(str){
      let data = {}
      if(this.linkName == '申请'){
          let fpm = []
          let fwpbh = []
          let fdw = []
          let kcList = []
          //console.log(this.dataSource)
          this.dataSource.forEach(item => {
          fpm.push(item.fwpmc)
          fwpbh.push(item.fwpbh)
          fdw.push(item.fdw)
          kcList.push({kcid:item.id,sqsl:item.fsl})
          })
          data={
          jobid: this.form.jobid,
          oaid: this.form.id,
          map: {
              id: this.form.id,
              fbz: this.form.fbz,
              fpm: fpm.toString(),
              fwpbh: fwpbh.toString(),
              fdw: fdw.toString(),
              kcList: kcList
          },
          }
      } else if(this.linkName == '科室审批'){
          data={
          jobid: this.form.jobid,
          oaid: this.form.id,
          map: {
              fksyj: this.form.fksyj,
              fksyjr: this.form.fksyjr,
              fksyjsj: this.form.fksyjsj
          },
          opinion: this.form.fksyj,
              currentNode: "",
              dbrid: "",
              lcmbCode: "",
              nextNode: "",
              nextNodeId: "",
              smgflag: ""
          }
      } else if(this.linkName == '办公室确认'){
          data={
          jobid: this.form.jobid,
          oaid: this.form.id,
          map: {
              fbgsyj: this.form.fbgsyj,
              fbgsyjr: this.form.fbgsyjr,
              fbgsyjsj: this.form.fbgsyjsj
          },
          opinion: this.form.fbgsyj,
          currentNode: "",
              dbrid: "",
              lcmbCode: "",
              nextNode: "",
              nextNodeId: "",
              smgflag: ""
          }
      }
      if(str == '保存'){
          saveFlow(data).then(res => {
          if(res.code == 200){
              this.$message.success(res.message)
              } else {
              this.$message.warning(res.message)
              }
          })
      } else if(str == '提交'){
          submitFlow(data).then(res => {
          if(res.code == 200){
              this.$message.success(res.message)
              this.closeVisible()
              } else {
              this.$message.warning(res.message)
              }
          })
      }
    },
    subAppTable(str){
        this.$refs.ruleForm.validate(valid => {
            if(valid) {
                let data = {}
                if(this.linkName == '申请'){
                    let fpm = []
                    let fwpbh = []
                    let fdw = []
                    let kcList = []
                    //console.log(this.dataSource)
                    this.dataSource.forEach(item => {
                    fpm.push(item.fwpmc)
                    fwpbh.push(item.fwpbh)
                    fdw.push(item.fdw)
                    kcList.push({kcid:item.id,sqsl:item.fsl})
                    })
                    data={
                    jobid: this.form.jobid,
                    oaid: this.form.id,
                    map: {
                        id: this.form.id,
                        fbz: this.form.fbz,
                        fpm: fpm.toString(),
                        fwpbh: fwpbh.toString(),
                        fdw: fdw.toString(),
                        kcList: kcList
                    },
                    }
                } else if(this.linkName == '科室审批'){
                    data={
                    jobid: this.form.jobid,
                    oaid: this.form.id,
                    map: {
                        fksyj: this.form.fksyj,
                        fksyjr: this.form.fksyjr,
                        fksyjsj: this.form.fksyjsj
                    },
                    opinion: this.form.fksyj,
                        currentNode: "",
                        dbrid: "",
                        lcmbCode: "",
                        nextNode: "",
                        nextNodeId: "",
                        smgflag: ""
                    }
                } else if(this.linkName == '办公室确认'){
                    data={
                    jobid: this.form.jobid,
                    oaid: this.form.id,
                    map: {
                        fbgsyj: this.form.fbgsyj,
                        fbgsyjr: this.form.fbgsyjr,
                        fbgsyjsj: this.form.fbgsyjsj
                    },
                    opinion: this.form.fbgsyj,
                    currentNode: "",
                        dbrid: "",
                        lcmbCode: "",
                        nextNode: "",
                        nextNodeId: "",
                        smgflag: ""
                    }
                }
                if(str == '保存'){
                    saveFlow(data).then(res => {
                    if(res.code == 200){
                        this.$message.success(res.message)
                        } else {
                        this.$message.warning(res.message)
                        }
                    })
                } else if(str == '提交'){
                    submitFlow(data).then(res => {
                    if(res.code == 200){
                        this.$message.success(res.message)
                        this.closeVisible()
                        } else {
                        this.$message.warning(res.message)
                        }
                    })
                }
            }
        })
    },
    editFslFuntion(record,fsl,isEditFsl,isEdit){
      //console.log(record)
      const dataSource = [...this.dataSource];
      const target = dataSource.find(item => item.key === record.key);
          //console.log('target',target)
      if (target) {
        if(record.isEdit == '编辑'){
          target[isEdit] = '保存'
          target[isEditFsl] = true;
        } else {
            if(this.form.id){
              let pD =  Number(record.fsl) >0 && Number(record.fsl) <= record.fwpsl?true:false
              if(pD){
                let data = {
                  sl: record.fsl,
                  sqmxid: record.id,
                }
                updatesqmx(data).then(res => {
                  //console.log(res)
                  if(res.code == 200){
                    this.$message.success(res.message)
                    target[isEdit] = '编辑'
                    target[isEditFsl] = false;
                    this.dataSource = dataSource;
                  } else {
                    this.$message.warning(res.message)
                  }
                })
              } else {
                this.$message.warning(`该物品当前可领取1- ${record.fwpsl} 件,请正确填写件数`)
              }
            } else{
              let pD =  Number(record.fsl) >0 && Number(record.fsl) <= record.fwpsl?true:false
              if(pD){
                target[fsl] = Number(record.fsl);
                target[isEdit] = '编辑'
                target[isEditFsl] = false;
                this.dataSource = dataSource;
              } else {
                this.$message.warning(`该物品当前可领取1- ${record.fwpsl} 件,请正确填写件数`)
              }
            }
          //console.log(this.dataSource)
        }
      }
    },
    //流转记录
    getflowJkPage(id){
      flowJkPage({oaid:id}).then(res => {
        let arr1 = []
            arr1 = res.result.records
        var arr2 = new Array();
        for (let i = arr1.length - 1; i >= 0; i--) {
          arr2.push(arr1[i]);
        }
        // //console.log('arr2',arr2)
        this.lzList = []
        arr1.forEach(item => {
          item = {
            ...item,
            key: item.jobid
          }
          this.lzList.push(item)
          this.imgList.forEach((items,a)=> {
            if(item.fdbrid){
              if(items.fhjmc == item.fhjmc){
                downMyQm({id:item.fdbrid}).then(resa => {
                  if(resa.code){
                    this.imgList[a].url = null
                  } else {
                    this.imgList[a].url = "data:image/png;base64," +
                    btoa( new Uint8Array(resa).reduce( (data, byte) => data + String.fromCharCode(byte), "" ))
                  }
                })
              }
            } else {}
          })
        })
      })
    },
    // 重置
    resetting(){
        this.dataSource = []
        this.selectedRowKeys = []
    },
    listOnSelectChange(selectedRowKeys,selectedRows){
        //console.log(selectedRowKeys,selectedRows)
        //console.log(this.listSelectedRowKeys)
        this.listSelectedRowKeys = selectedRowKeys
    },
    // 批量删除
    deletes(){
        if(this.listSelectedRowKeys.length == 0){
            //console.log(this.listSelectedRowKeys.length)
            this.$message.warning('请选择删除项')
        } else {
            this.listSelectedRowKeys.forEach(item => {
            //     this.dataSource = this.dataSource.filter(items => items.key !== item)
            this.start(item)
            })
        }
    },
    //
    searchKclist(e){
        //console.log(this.kcName)
        kcList({fwpmc:this.kcName}).then(res => {
            this.dataB = []
            res.result.records.forEach((item,i) => {
                item = {
                    ...item,
                    key: item.fwpbh,
                }
                this.dataB.push(item)
            });
        })
    },
        // 退回意见
        returnClick(){
            this.$refs.ruleForm.validate(valid => {
                if (valid){
                    queryAllYbJob({jobid:this.form.jobid}).then(res => {
                        let list = res.result
                        list.forEach(item => {
                            if(item.fhjmc == '申请'){
                                this.returnFuntion(this.form.jobid,item.id)
                            }
                        })
                    })
                }
            })
        },
        returnFuntion(jobid,ids){
            let returnStr = ''
            if(this.linkName == '科室审批'){
                returnStr = this.form.fksyj
            } else if(this.linkName == '办公室确认'){
                returnStr = this.form.fbgsyj
            }
            let data = {
                jobid: jobid,
                opinion: returnStr,
                shjid: ids,
                smgflag: '',
                smsContent: '',
            }
            returnUpperzdyFlowJob(data).then(res => {
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.closeVisible()
                } else {
                    this.$message.error(res.message)
                }
            })
        },
  },
//   updated() {
//     this.$nextTick(() => {
//         if(this.linkName == '科室审批'){
//           this.$refs.refjyInput.focus()
//         } else if(this.linkName == '办公室确认'){
//           this.$refs.refjyeInput.focus()
//         }
//         this.reviewRecordsFocusEvent()
//     })
//   },
  watch:{
    // formIndex(newval,oldVal){
    //   //console.log('formIndex',newval)
    //   this.form = {...newval}
    // },
    dataSource(newval,oldVal){
        this.checkListName = ''
        let list = []
        newval.forEach(item => {
            list.push(item.fwpmc)
        })
        this.checkListName = list.toString()
    },
    formData(newval,oldVal){
      //console.log(newval)
      this.form = {...newval}
      // this.formIndex = {...newval}
      let id = ''
      if(this.form.oaid){
        id = this.form.oaid
      } else {
        id = this.form.id
      }
      this.getflowJkPage(id)
    },
    Mxlist(newval,oldVal){
      //console.log('Mxlist变变变',newval)
      //console.log('Mxlist=========',this.Mxlist)
      // //console.log('formIndex',this.formIndex)
      // this.form.id = this.formIndex.id
      // this.form.fbz = this.formIndex.fbz
      if(this.linkName=='申请'){
        this.dataSource = []
        this.selectedRowKeys = []
        newval.forEach(item => {
          queryById({id:item.fkcid}).then(res => {
            item = {
              ...item,
              fwplb: res.result.fwplb,
              fwpsl: res.result.fwpsl,
              fwpmc: item.fsqmc,
              key: item.fwpbh,
              fsl: item.fsqsl,
              isEdit: '编辑',
              isEditFsl: Boolean(false)
            }
            this.dataSource.push(item)
            this.dataSource = [...this.dataSource];
            this.dataSource.forEach((item,i) => {
              this.dataB.forEach(items => {
                if(item.fwpbh == items.fwpbh){
                  this.dataSource[i].key = items.key
                  this.selectedRowKeys.push(items.key)
                }
              })
            })
          })
        })
      } else {
        newval.forEach(item => {
          this.tableList.push({
          })
        })
      }
    },
    deep: true
  }
}
</script>
<style>
@import url('../../css/oaLess.css');
@import url('../../css/oaClass.css');
</style>
